/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.ui.PlayingTrackInfo');

goog.require('kramerius.audio.ui.PlayerComponent');
goog.require('goog.dom');
goog.require('goog.Timer');

/** @const */ 
var TRACK_INFO_LENGTH = 30;
/** @const */ 
var SPACES_AFTER = 5;


/**
 * @constructor 
 * @param {Element} container Element containing this component.
 * @param {kramerius.audio.Controller} controller Controller of the audio player.
 */
kramerius.audio.ui.PlayingTrackInfo = function(container, controller){
    kramerius.audio.ui.PlayerComponent.call(this, container, controller);
    this.text_ = undefined;
    this.windowStart_ = 0;
    this.windowEnd_ = TRACK_INFO_LENGTH -1 ;
    //this.logWindow_();
    this.controller_.setPlayingTrackInfo(this);
    this.timer_ = this.initializeTimer_();
};
goog.inherits(kramerius.audio.ui.PlayingTrackInfo, kramerius.audio.ui.PlayerComponent);


/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.initializeTimer_ = function(){
    var timer = new goog.Timer(100);
    goog.events.listen(timer, goog.Timer.TICK, 
        function(){
            this.slide_();
        }, false, this);
    return timer;
}

/** @public **/
kramerius.audio.ui.PlayingTrackInfo.prototype.setText = function(text){
    this.text_ = this.padText_(text);
    this.render_();
};

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.resetWindow_ = function(){
    this.windowStart_ = 0;
    this.windowEnd_ = TRACK_INFO_LENGTH -1 ;
}

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.updateWindow_ = function(){
    this.windowStart_ = this.moduloPlusOne_(this.windowStart_);
    this.windowEnd_ = this.moduloPlusOne_(this.windowEnd_);
//console.log("window updated");
//this.logWindow_();
}

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.logWindow_ = function(){
    console.log("window: <" + this.windowStart_ + "," + this.windowEnd_ + ">");
}

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.moduloPlusOne_ = function(index){
    if (index == this.text_.length -1){
        return 0;
    }else{
        return index+1;
    }
}

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.padText_ = function(text){
    //console.log("padding text '" + text +"'");
    if (text.length > TRACK_INFO_LENGTH){
        //return text;
        return this.appendSpaces_(text, SPACES_AFTER)
    }else{
        var difference = TRACK_INFO_LENGTH - text.length;
        return this.appendSpaces_(text, difference + SPACES_AFTER);
//        var emptySpace = ' ';
//        var buffer = [text];
//        for(var i=0; i<difference; i++){
//            buffer.push(emptySpace);
//        }
//        return buffer.join('');
    }
}

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.appendSpaces_ = function(text, spaces){
    //var emptySpace = '.';
    var emptySpace = ' ';
    var buffer = [text];
    for(var i=0; i<spaces; i++){
        buffer.push(emptySpace);
    }
    return buffer.join('');
}



/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.render_ = function(){
    if (this.text_ != undefined){
        var text = this.windowContent_();
        //console.log("window content: '" + text + "', size: " + text.length);
        this.setContent_(text);
    }else{
        this.setContent_('');
    }
};


/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.windowContent_ = function(){
    var buffer = [];
    var first = this.windowStart_;
    var last = this.windowEnd_;
    //    console.log("first: " + first);
    //    console.log("last: " + last);
    for(var i = first; i!= last; i=this.moduloPlusOne_(i)){
        //console.log("i: " + i);
        var character = this.text_.charAt(i);
        buffer.push(character);
    }
    var lastChar = this.text_.charAt(i);
    buffer.push(lastChar);
    return buffer.join('');
}


/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.setContent_ = function(content){
    goog.dom.setTextContent(this.container_, content);
};

kramerius.audio.ui.PlayingTrackInfo.prototype.animate = function(){
    this.timer_.start();
};

/** @private **/
kramerius.audio.ui.PlayingTrackInfo.prototype.slide_ = function(){
    this.updateWindow_();
    this.render_();
}

kramerius.audio.ui.PlayingTrackInfo.prototype.pauseAnimation = function(){
    this.timer_.stop();
};

kramerius.audio.ui.PlayingTrackInfo.prototype.resetEmpty = function(){
    this.timer_.stop();
    this.text_ = undefined;
    this.resetWindow_();
    this.render_();
}

kramerius.audio.ui.PlayingTrackInfo.prototype.resetText = function(){
    this.timer_.stop();
    this.resetWindow_();
    this.render_();
}

